import {
  Toast, ImagePreview
} from 'vant';
import { router } from '@/router/permission';

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  let date
  if (typeof time === 'object') {
    date = time
  } else {
    if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
      time = parseInt(time)
    }
    if ((typeof time === 'number') && (time.toString().length === 10)) {
      time = time * 1000
    }
    date = new Date(time)
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  }
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key]
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] }
    if (result.length > 0 && value < 10) {
      value = '0' + value
    }
    return value || 0
  })
  return time_str
}

/**
 * 手机号校验
 * @param str
 * @returns {boolean}
 */
export const checkPhone = function (val) {
  const reg = /^1[23456789]\d{9}$/;
  if (!reg.test(val)) {
    return false
  } else {
    return true
  }
}

// 轮播图点击
export const bannerToParams = (item) => {
  console.log(item.jump_type);
  if (item.jump_type === 1) {
    //图片预览
    ImagePreview([item.jump_content]);
  } else if (item.jump_type === 2) {
    // 网页地址
    window.location.href = item.jump_content;
  } else if (item.jump_type === 4) {
    if (item.jump_content == 'goods_list_type') {
      localStorage.setItem('shopName', item.name)
      router.push({
        name: 'newArea',
        query: {
          jump_content: item.jump_content,
          type: Number(item.jump_params)
        }
      })

    } else if (item.jump_content == 'goods_detail') {
      router.push({
        path: '/good?id=' + item.jump_params
      })
    } else if (item.jump_content == 'goods_list_mcate') {
      //cate_id  jump_params
      localStorage.setItem('shopInfoName', item.name)
      router.push({
        path: `/shop-info?cate_id=${item.jump_params}`
      })

    }else if(item.jump_content=='goods_list_flag'){
      localStorage.setItem('shopName', item.name)
      router.push({
        name: 'newArea',
        query: {
          jump_content: item.jump_content,
          type: Number(item.jump_params)
        }
      })
    }
  } else if (item.jump_type === 5) {
    // console.log(33);
    localStorage.setItem('content', item.jump_content)
    router.push({
      path: '/content?title=' + item.name,
    })
  }
}

export const formatSubsidy = (value) => {
  const subsidy = Number(value) || 0;

  // 转换为字符串，确保有至少两位小数
  let formattedSubsidy = subsidy.toString();

  if (formattedSubsidy.includes('.')) {
    // 如果有小数点，截取到两位小数
    formattedSubsidy = formattedSubsidy.split('.')[0] + '.' + formattedSubsidy.split('.')[1].slice(0, 2);
  } else {
    // 如果没有小数点，添加 .00
    formattedSubsidy += '.00';
  }

  // 如果小数部分少于两位，补零
  if (!formattedSubsidy.includes('.')) {
    formattedSubsidy += '.00';
  } else {
    const decimalPart = formattedSubsidy.split('.')[1];
    if (decimalPart.length < 2) {
      formattedSubsidy += '0'; // 补零
    }
  }

  console.log(formattedSubsidy);

  return formattedSubsidy;
}